const Router = require('koa-router');
const router = new Router();
const passport = require('koa-passport')

// 引入模板实例
const Profile = require('../../models/Profile')

// 引入验证
const validationProfileIpt = require('../../validation/profile')

/**
 * @route GET api/profile/test
 * @desc 测试接口
 * @access 接口是公开的
 * 
 */
 router.get("/test", async ctx => {
  ctx.status = 200;
  ctx.body = {
    msg: 'profile works...'
  };
})

/**
 * @route GET api/profile
 * @desc 个人信息接口地址
 * @access 接口是私有的
 * 
 */
 router.get("/", passport.authenticate('jwt', { session: false }), async ctx => {
  //  console.log(ctx.state.user);
  // 跨表联查populate
  const profile = await Profile.find({ user:ctx.state.user.id }).populate('user', [
    'name',
    'avatar'
  ])
  // console.log(profile);
  if(profile.length > 0 ){
    ctx.status = 200;
    ctx.body = profile
  }else{
    ctx.status = 404;
    ctx.body = { noprofile:"该用户没有任何相关信息" }
    return;
  }
})

/**
 * @route POST api/profile
 * @desc 添加和编辑个人信息接口地址
 * @access 接口是私有的
 * 
 */
 router.post("/", passport.authenticate('jwt', { session: false }), async ctx => {

  const { errors, isValid } = validationProfileIpt(ctx.request.body);
  // 判断是否验证码通过
  if(!isValid){
    ctx.status = 400;
    ctx.body = errors;
    return;
  }

  const profileFields = {};
  profileFields.user = ctx.state.user.id;
  if(ctx.request.body.handle){
    profileFields.handle = ctx.request.body.handle;
  }
  if(ctx.request.body.company){
    profileFields.company = ctx.request.body.company;
  }
  if(ctx.request.body.location){
    profileFields.location = ctx.request.body.location;
  }
  if(ctx.request.body.status){
    profileFields.status = ctx.request.body.status;
  }
  // skills 数据转换 "html,css,js,vue,node"
  if(typeof ctx.request.body.skills !== "undefined"){
    profileFields.skills = ctx.request.body.skills.split(",");
  }

  if(ctx.request.body.educations){
    profileFields.educations = ctx.request.body.educations;
  }
  // social
  profileFields.social = {}
  if(ctx.request.body.wechat){
    profileFields.social.wechat = ctx.request.body.wechat;
  }
  if(ctx.request.body.QQ){
    profileFields.social.QQ= ctx.request.body.QQ;
  }

  // 查询数据库
  const profile = await Profile.find({ user:ctx.state.user.id })
  if(profile.length > 0){
    // 编辑更新
    const profileUpdate = await Profile.findOneAndUpdate(
      { user: ctx.state.user.id },
      { $set: profileFields },
      { new: true }
    );
    ctx.body = profileUpdate
  }else{
    // 添加
    await new Profile(profileFields).save().then(profile => {
      ctx.status = 200;
      ctx.body = profile;
    });
  }
})


/**
 * @route GET api/profile/user?user_id=test1
 * @desc  通过user_id 获取个人信息地址
 * @access 接口是公开的
 */
 router.get("/user", async ctx => {
  const errors = {}
  const user_id = ctx.query.user_id;
  console.log(user_id);
  const profile = await Profile.find({ user: user_id }).populate("user", ["name","avatar"])
  // console.log(profile);
  if(profile.length < 1){
    errors.noprofile = "未找到该用户的信息"
    ctx.status = 404;
    ctx.body = errors
  }else{
    ctx.body = profile[0]
  }
})


/**
 * @route GET api/profile/all
 * @desc  获取所有人信息地址
 * @access 接口是公开的
 */
 router.get("/all", async ctx => {
  const errors = {}
  // 这里是{} 空对象，查到所有 
  const profiles = await Profile.find({}).populate("user", ["name","avatar"])
  // console.log(profile);
  if(profiles.length < 1){
    errors.noprofile = "没有任何用户的信息"
    ctx.status = 404;
    ctx.body = errors
  }else{
    ctx.body = profiles
  }
})

module.exports = router.routes();